package com.lvtulife.paas.gtd.service.impl;

import com.lvtulife.common.bean.DelVo;
import com.lvtulife.common.component.constants.Constant;
import com.lvtulife.common.component.easyui.Tree;
import com.lvtulife.common.component.exception.ErrMsg;
import com.lvtulife.paas.gtd.bean.AimTrunkVo;
import com.lvtulife.paas.gtd.bean.AimVo;
import com.lvtulife.paas.gtd.entity.GtdAim;
import com.lvtulife.paas.gtd.service.GtdAimServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lvtulife.paas.gtd.mapper.GtdAimTrunkMapper;
import com.lvtulife.paas.gtd.entity.GtdAimTrunk;
import com.lvtulife.paas.gtd.service.GtdAimTrunkServiceI;
import com.lvtulife.common.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * GtdAimTrunk 表数据服务层接口实现类
 */
@Service
@Transactional(readOnly = true)
public class GtdAimTrunkServiceImpl extends BaseServiceImpl<GtdAimTrunkMapper, GtdAimTrunk> implements GtdAimTrunkServiceI {

    @Autowired
    private GtdAimServiceI aimService;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Long saveTrunkAim(AimTrunkVo vo) {
        GtdAimTrunk entity = initBean(vo);

        /* 保存目标内容 */
        Long aimId = aimService.saveAim(vo.getAimVo());

        /* 保存主干信息 */
        entity.setAid(aimId);
        int count = vo.getId() != null ? baseMapper.updateSelectiveById(entity) : baseMapper.insert(entity);

        checkOperation(count);
        return entity.getId();
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void sortTrunkAim(Long id, Integer sort) {
        /* 校验参数与操作对象 */
        GtdAimTrunk entity = verifyEntity(new GtdAimTrunk(id));

        /* 设置更新内容 */
        entity.setSort(sort == null || sort <= 0 ? 1 : sort);
        updateSelectiveEntity(entity);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void setParentTrunkAim(Long id, Long parentId) {
        GtdAimTrunk parentEntity = verifyEntity(new GtdAimTrunk(parentId));

        GtdAimTrunk entity = verifyEntity(new GtdAimTrunk(id));
        entity.setPid(parentEntity.getId());
        updateSelectiveEntity(entity);
    }

    @Override
    public void setPartTrunkAim(Long id, Integer partUnit, Integer partValue) {
        GtdAimTrunk entity = getEntity(id);

        //if(Constant.Gtd.AimUnit.YEAR == partUnit)


    }

    @Override
    public void delTrunkAim(DelVo vo) {

    }

    @Override
    public List<AimTrunkVo> findPartTrunkAims(Long userId, Integer partUnit, Integer partValue) {
        return null;
    }

    @Override
    public List<AimTrunkVo> findSonTrunkAims(Long pid) {
        return null;
    }

    @Override
    public List<AimTrunkVo> findTypeTrunkAims(Long userId, Long typeId) {
        return null;
    }

    @Override
    public List<AimTrunkVo> findLikeTrunkAims(Long userId, String likeStr) {
        return null;
    }

    @Override
    public AimTrunkVo getTrunkAim(Long id) {
        return null;
    }

    @Override
    public List<AimTrunkVo> getFullTrunkAims(Long id) {
        return null;
    }

    @Override
    public Tree getTreeTrunkAims(Long id) {
        return null;
    }


    private GtdAimTrunk initBean(AimTrunkVo vo) {
        verifyParams(vo);

        GtdAimTrunk entity = new GtdAimTrunk();
        if (vo.getId() != null) {
            /* 读取目标内容主键 */
            entity = getEntity(vo.getId());
            vo.getAimVo().setId(entity.getAid());

            entity = new GtdAimTrunk(vo.getId());
            entity.setUpdatedDt(new Date());
            /* 可修改项 */
            if (vo.getPartUnit() != null) {
                verifyConstant("Gtd.AimUnit", vo.getPartUnit());
                entity.setPartUnit(vo.getPartUnit());
            }
            if (vo.getPartValue() != null)
                entity.setPartValue(vo.getPartValue());
        } else {
            verifyParams(vo.getPartUnit(), vo.getPartValue());

            /* 验证参数合法性 */
            verifyConstant("Gtd.AimUnit", vo.getPartUnit());

            /* 必填项 */
            entity.setPartUnit(vo.getPartUnit());
            entity.setPartValue(vo.getPartValue());

            /* 默认项 */
            entity.setSort(1);
            entity.setCreatedDt(new Date());
            entity.setUpdatedDt(entity.getCreatedDt());
            entity.setPid(vo.getPid() == null ? 0L : vo.getPid());
        }

        return entity;
    }

    /* 使用此方法必须在调用方加上事务处理注解 */
    private void updateEntity(GtdAimTrunk entity) {
        entity.setUpdatedDt(new Date());
        checkOperation(updateById(entity));
    }

    /* 使用此方法必须在调用方加上事务处理注解 */
    private void updateSelectiveEntity(GtdAimTrunk entity) {
        entity.setUpdatedDt(new Date());
        checkOperation(updateSelectiveById(entity));
    }
}